package com.yyq.vod.vodService.Imp;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.utils.StringUtils;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.yyq.servicebase.exceptionhandler.YyqException;
import com.yyq.vod.Utils.AliyunVodSDKUtils;
import com.yyq.vod.Utils.ConstantPropertiesUtil;
import com.yyq.vod.vodService.vodService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import static org.reflections.Reflections.log;

/**
 * @author yyq
 * @create 2021-04-21 0:48
 */
@Service
public class vodServiceImp implements vodService {
    @Override
    public String upfile(MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream(); //获取文件流
            String originalFilename = file.getOriginalFilename(); //获取文件原始名称
            String title = originalFilename.substring(0, originalFilename.lastIndexOf("."));
            //分割

            UploadStreamRequest request = new UploadStreamRequest(
                    ConstantPropertiesUtil.ACCESS_KEY_ID,
                    ConstantPropertiesUtil.ACCESS_KEY_SECRET,
                    title, originalFilename, inputStream);

            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);

            //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。
            // 其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
            String videoId = response.getVideoId();
            if (!response.isSuccess()) {
                String errorMessage = "阿里云上传错误：" + "code：" + response.getCode() + ", message：" + response.getMessage();
                log.warn(errorMessage);
                if(StringUtils.isEmpty(videoId)){
                    throw new YyqException(20001, errorMessage);
                }
            }

            return videoId;
        } catch (IOException e) {
            throw new YyqException(20001, "guli vod 服务上传失败");
        }

    }

    @Override
    public void deleteMoreVideo(List<String> videoList) {
        try {
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(ConstantPropertiesUtil.ACCESS_KEY_ID, ConstantPropertiesUtil.ACCESS_KEY_SECRET);
            DeleteVideoRequest request=new DeleteVideoRequest();
            //videoList->转化为String 1,2,3,4形式
            String join = org.apache.commons.lang.StringUtils.join(videoList, ",");
            request.setVideoIds(join);
            client.getAcsResponse(request);

        }
        catch (Exception e){
            throw new YyqException(20001,"视频删除异常");
        }
    }
//
//    public static void main(String[] args) {
//        List<String> list=new ArrayList<>();
//        list.add("11");
//        list.add("22");
//        String join = org.apache.commons.lang.StringUtils.join(list, ",");
//        System.out.println(join);
//    }
}
